K=src/kernel
U=src/user

OBJS = \
  $K/entry.o	\
  $K/start.o	\
  $K/console.o 	\
  $K/printf.o 	\
  $K/uart.o		\
  $K/kalloc.o 	\
  $K/string.o 	\
  $K/main.o 	\
  $K/vm.o 		\
  $K/proc.o 	\
  $K/swtch.o 	\
  $K/trampoline.o 	\
  $K/trap.o 	\
  $K/syscall.o 	\
  $K/bio.o 		\
  $K/fs.o 		\
  $K/log.o 		\
  $K/file.o 	\
  $K/exec.o 	\
  $K/sysfile.o 	\
  $K/kernelvec.o	\
  $K/plic.o 	\
  $K/virtio_disk.o 	

ifndef TOOLPREFIX
TOOLPREFIX := $(shell if riscv64-unknown-elf-objdump -i 2>&1 | grep 'elf64-big' >/dev/null 2>&1; \
	then echo 'riscv64-unknown-elf-'; \
	elif riscv64-linux-gnu-objdump -i 2>&1 | grep 'elf64-big' >/dev/null 2>&1; \
	then echo 'riscv64-linux-gnu-'; \
	elif riscv64-unknown-linux-gnu-objdump -i 2>&1 | grep 'elf64-big' >/dev/null 2>&1; \
	then echo 'riscv64-unknown-linux-gnu-'; \
	else echo "***" 1>&2; \
	echo "*** Error: Couldn't find a riscv64 version of GCC/binutils." 1>&2; \
	echo "*** To turn off this error, run 'gmake TOOLPREFIX= ...'." 1>&2; \
	echo "***" 1>&2; exit 1; fi)
endif

QEMU = qemu-system-riscv64

CC = $(TOOLPREFIX)gcc
AS = $(TOOLPREFIX)gas
LD = $(TOOLPREFIX)ld
OBJCOPY = $(TOOLPREFIX)objcopy
OBJDUMP = $(TOOLPREFIX)objdump

# 从左到右：开启所有警告 警告信息视为错误 编译器优化 进制编译器优化掉帧指针 开启GDB调试信息 生成DWARF2调试信息
CFLAGS = -Wall -Werror -O -fno-omit-frame-pointer -ggdb -gdwarf-2
# 生成依赖关系文件
CFLAGS += -MD
# 使用medium model内存模型，允许程序使用2GB内存
CFLAGS += -mcmodel=medany
# 编译器将生成一个没有主机操作系统支持的独立程序，且没有libc支持 禁止通用块 不使用标准库 不适用指令优化
CFLAGS += -ffreestanding -fno-common -nostdlib -mno-relax
# 添加当前目录到头文件搜索路径中
CFLAGS += -I.
# 动态添加fno-stack-protector标志，禁用栈保护
CFLAGS += $(shell $(CC) -fno-stack-protector -E -x c /dev/null >/dev/null 2>&1 && echo -fno-stack-protector)

# 禁用PIE功能（将可执行文件加载到内存的任意位置都可执行），针对Ubuntu 16.10
ifneq ($(shell $(CC) -dumpspecs 2>/dev/null | grep -e '[^f]no-pie'),)
CFLAGS += -fno-pie -no-pie
endif
ifneq ($(shell $(CC) -dumpspecs 2>/dev/null | grep -e '[^f]nopie'),)
CFLAGS += -fno-pie -nopie
endif

# 最大页大小为4KB
LDFLAGS = -z max-page-size=4096

all: $K/kernel

$K/kernel: $(OBJS) $K/kernel.ld $U/initcode 
	$(LD) $(LDFLAGS) -T $K/kernel.ld -o $K/kernel $(OBJS)
	$(OBJDUMP) -S $K/kernel > $K/kernel.asm
	$(OBJDUMP) -t $K/kernel | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > $K/kernel.sym

$U/initcode: $U/initcode.S 
	$(CC) $(CFLAGS) -march=rv64g -nostdinc -I. -Isrc/kernel -c $U/initcode.S -o $U/initcode.o 
	$(LD) $(LDFLAGS) -N -e start -Ttext 0 -o $U/initcode.out $U/initcode.o 
	$(OBJCOPY) -S -O binary $U/initcode.out $U/initcode
	$(OBJDUMP) -S $U/initcode.o > $U/initcode.asm

# tags: $(OBJS) _init
# 	etags *.S *.c

ULIB = $U/ulib.o $U/usys.o $U/printf.o $U/umalloc.o 

_%: %.o $(ULIB)
	$(LD) $(LDFLAGS) -T $U/user.ld -o $@ $^
	$(OBJDUMP) -S $@ > $*.asm
	$(OBJDUMP) -t $@ | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > $*.sym 

$U/usys.S : $U/usys.pl 
	perl $U/usys.pl > $U/usys.S 

$U/usys.o : $U/usys.S 
	$(CC) $(CFLAGS) -c -o $U/usys.o $U/usys.S 

# $U/_forktest: $U/forktest.o $(ULIB)
# 	$(LD) $(LDFLAGS) -N -e main -Ttext 0 -o $U/_forktest $U/forktest.o $U/ulib.o $U/usys.o
# 	$(OBJDUMP) -S $U/_forktest > $U/forktest.asm

# 暂时加上使用xv6的img
src/mkfs/mkfs: src/mkfs/mkfs.c $K/fs.h $K/param.h 
	gcc -Werror -Wall -I. -o src/mkfs/mkfs src/mkfs/mkfs.c 

.PRECIOUS: %.o 

UPROGS=\
	$U/_init
# 	$U/_cat		\
# 	$U/_echo 	\
# 	$U/_forktest\
# 	$U/_grep	\
# 	$U/_init	\
# 	$U/_kill	\
# 	$U/_ln		\
# 	$U/_ls		\
# 	$U/_mkdir	\
# 	$U/_rm		\
# 	$U/_sh		\
# 	$U/_stressfs\
# 	$U/_usertests	\
# 	$U/_grind	\
# 	$U/_wc 		\
# 	$U/_zombie	\

fs.img: src/mkfs/mkfs $(UPROGS)
	src/mkfs/mkfs fs.img $(UPROGS)

-include src/kernel/*.d src/user/*.d 

clean:
	rm -f *.tex *.dvi *.idx *.aux *.log *.ind *.ilg \
		  */*.o */*.d */*.asm */*.sym \
		  $U/initcode $U/initcode.out $K/kernel fs.img \
		  mkfs/mkfs .gdbinit \
		  $U/usys.S \
		  $(UPROGS)

# 得到GDB端口号
GDBPORT = $(shell expr `id -u` % 5000 + 25000)
# 根据当前版本的QEMU是否有-gdb选项判断使用GDB的时候是否加上-gdb选项参数
# QEMUGDB = $(shell if $(QEMU) -help | grep -q '^-gbd'; \
	then echo "-gdb tcp::$(GDBPORT)"; \
	else echo "-s -p $(GDBPORT)"; fi)
QEMUGDB = -gdb tcp::$(GDBPORT)
ifndef CPUS 
CPUS := 1
endif

QEMUOPTS = -machine virt -bios none -kernel $K/kernel -m 128M -smp $(CPUS) -nographic
QEMUOPTS += -global virtio-mmio.force-legacy=false
QEMUOPTS += -drive file=fs.img,if=none,format=raw,id=x0
QEMUOPTS += -device virtio-blk-device,drive=x0,bus=virtio-mmio-bus.0

qemu:$K/kernel fs.img
	$(QEMU) $(QEMUOPTS)

.gdbinit: .gdbinit.tmpl-riscv
	sed "s/:1234/:$(GDBPORT)/" < $^ > $@

qemu-gdb: $K/kernel .gdbinit fs.img 
	@echo "***Now run 'gdb' in another window." 1>&2
	$(QEMU) $(QEMUOPTS) -S $(QEMUGDB)